package com.ax.base.drugproducer.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.BuildTree;
import com.ax.base.common.utils.PageUtils;
import com.ax.base.common.utils.PinYinUtil;
import com.ax.base.common.utils.Query;
import com.ax.base.drugproducer.dao.BaseDrugMapper;
import com.ax.base.drugproducer.domain.BaseDrug;
import com.ax.base.drugproducer.service.DrugService;
import com.ax.base.system.domain.MenuDO;

@Service
public class DrugServiceImpl implements DrugService {
	@Autowired
	BaseDrugMapper baseDrugMapper;
	
	/**
	 * 药品查询(模块:药品)*包含药品的药品分类信息
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return PageUtils
	 */
	@Override
	public List<BaseDrug> queryDrugSer(Map<String, Object> params){
	//	Query query = new Query(params);
		List<BaseDrug> baseDrugManagers = baseDrugMapper.queryDrugMapper(params);
	//	int total = baseDrugMapper.count(query);
	//	PageUtils pageUtil = new PageUtils(baseDrugManagers,total);
		return baseDrugManagers;
	}

	/**
	 * 药品保存(模块:药品)
	 * 
	 * @param BaseDrug baseDrug
	 *           
	 * @return SUCC
	 */
	@Override
	public void insertBaseDrugSer(BaseDrug baseDrug) {
		baseDrug.setFirstLetter(PinYinUtil.getFirstSpell(baseDrug.getDrugName()));
		String drugClassName = baseDrug.getDrugClassCode().substring(0,baseDrug.getDrugClassCode().indexOf("-"));
		String drugClassCode = baseDrug.getDrugClassCode().substring(baseDrug.getDrugClassCode().indexOf("-") + 1);
		baseDrug.setDrugClassCode(drugClassCode);
		baseDrug.setDrugClassName(drugClassName);
		baseDrugMapper.insertSelective(baseDrug);
	}

	/**
	 * 药品修改(模块:药品)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return HTML
	 */
	@Override
	public BaseDrug getDrugSer(Long id) {
		return baseDrugMapper.selectByPrimaryKey(id);
	}

	/**
	 * 药品修改(模块:药品)
	 * 
	 * @param BaseDrug baseDrug
	 *           
	 * @return SUCC
	 */
	@Override
	public void updateBaseDrugSer(BaseDrug baseDrug) {
		baseDrug.setFirstLetter(PinYinUtil.getFirstSpell(baseDrug.getDrugName()));
		String drugClassName = baseDrug.getDrugClassCode().substring(0,baseDrug.getDrugClassCode().indexOf("-"));
		String drugClassCode = baseDrug.getDrugClassCode().substring(baseDrug.getDrugClassCode().indexOf("-") + 1);
		baseDrug.setDrugClassCode(drugClassCode);
		baseDrug.setDrugClassName(drugClassName);
		baseDrugMapper.updateByPrimaryKeySelective(baseDrug);
	}

	/**
	 * Tree
	 * 
	 * @param 无
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getTree() {
		List<Tree<MenuDO>> deps = baseDrugMapper.listClass();
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}

	/**
	 * 药品端树形选择Tree
	 * 
	 * @param String drugClassCode
	 *           
	 * @return Tree<MenuDO>
	 */
	@Override
	public Tree<MenuDO> getbaseDrugOtherTree(String drugClassCode){
		String code = drugClassCode;
		List<Tree<MenuDO>> deps = baseDrugMapper.listClass();
		for(int i = 0 ;i < deps.size();i++){
			Map<String, Object> state = new HashMap<>();
			String text = deps.get(i).getText();
			text = text.substring(text.indexOf("-") + 1);
			if (text.equals(code)) {
				state.put("selected", true);
			} else {
				state.put("selected", false);
			}
			deps.get(i).setState(state);
		}
		Tree<MenuDO> t = BuildTree.build(deps);
		return t;
	}
}
